package com.turnengine.client.global.game.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.global.game.enums.GameStatus;
import java.io.IOException;

/**
 * The Game Instance Serializer.
 */
public class GameInstanceSerializer implements IGameInstanceSerializer {

	@Override
	public IGameInstance readObject(IDataReader reader) throws IOException {

		// Read fields
		int id = reader.readInt();
		String name = reader.readString(true);
		int gameId = reader.readInt();
		int versionId = reader.readInt();
		int hostId = reader.readInt();
		GameStatus status = reader.readEnum(GameStatus.class, true);

		// Create object
		IGameInstance object = new GameInstance();
		object.setId(id);
		object.setName(name);
		object.setGameId(gameId);
		object.setVersionId(versionId);
		object.setHostId(hostId);
		object.setStatus(status);
		return object;
	}

	public void writeObject(IDataWriter writer, IGameInstance object) throws IOException {

		// Get fields
		int id = object.getId();
		String name = object.getName();
		int gameId = object.getGameId();
		int versionId = object.getVersionId();
		int hostId = object.getHostId();
		GameStatus status = object.getStatus();

		// Write fields
		writer.writeInt(id);
		writer.writeString(name, true);
		writer.writeInt(gameId);
		writer.writeInt(versionId);
		writer.writeInt(hostId);
		writer.writeEnum(status, true);
	}
}
